#!/bin/bash

start_deprecation() {
	distro=$1
	echo
	printf "========================================== (\033[91;1m%s start\033[0m) ==========================================\n" "$distro" 
	echo
}

finish_deprecation() {
	distro=$1
	echo
	printf "==========================================  (\033[91;1m%s finish\033[0m) ==========================================\n" "$distro" 
	echo
}

if [ -f /etc/os-release ]; then

    source /etc/os-release

    if [ "$ID" = "ubuntu" ]; then

        echo "This is Ubuntu system."
        start_deprecation "Ubuntu"
        start_deprecation "install necessary packages"
        apt install -y wget curl git vim zsh
        finish_deprecation "install necessary packages"
    elif [ "$ID" = "centos" ]; then

        echo "This is CentOS system."
        start_deprecation "change yum repo"
        sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://vault.centos.org/centos|g' -i.zsh /etc/yum.repos.d/CentOS-Base.repo
        finish_deprecation "change yum repo"
        start_deprecation "install necessary packages"
        yum install -y wget curl git vim zsh
        finish_deprecation "install necessary packages"
    else
        printf "\033[91;1mThis is neither Ubuntu nor CentOS system\033[0m\n"
        exit 1
    fi
else
    printf "\033[91;1mCould not find the os-release file.\033[0m\n"
    exit 1
fi


start_deprecation  "download oh-my-zsh"
sh -c "$(curl -fsSL https://ghfast.top/https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | sed 's|https://github.com|https://ghfast.top/https://github.com|g')"
finish_deprecation  "download oh-my-zsh"

start_deprecation "switch to zsh"
chsh -s /bin/zsh
zsh
finish_deprecation "switch to zsh"

start_deprecation "download zsh-autosuggestions"
git clone https://ghfast.top/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
finish_deprecation "download zsh-autosuggestions"

start_deprecation "fix theme"
# fix theme
sed -i 's/robbyrussell/ys/g' $HOME/.zshrc
finish_deprecation "fix theme"

start_deprecation "add plugins"
# add plugins
sed -i 's/plugins=(git)/plugins=(git history history-substring-search node npm wd web-search last-working-dir zsh-autosuggestions vi-mode)/g' $HOME/.zshrc
finish_deprecation "add plugins"

start_deprecation "flush zshrc"
source $HOME/.zshrc
finish_deprecation "flush zshrc"



if [ -f /etc/os-release ]; then
    if [ "$ID" = "ubuntu" ]; then
        echo "This is Ubuntu system."
    elif [ "$ID" = "centos" ]; then
        echo "This is CentOS system."
        start_deprecation "change back to original repo"
        mv /etc/yum.repos.d/CentOS-Base.repo.zsh /etc/yum.repos.d/CentOS-Base.repo
        finish_deprecation "change back to original repo"
    else
        echo "This is neither Ubuntu nor CentOS system."
        exit
    fi
else
    echo "Could not find the os-release file."
    exit
fi
